﻿/***************************************************************
* File    : EgtSensor.cs
* Author  : Kyle Burgess
*
* Description:
*   View that displays sensor data
*
* History :
***************************************************************/

using System;
using System.Threading;

namespace RLGDashboard
{
    public class EgtSensor : Sensor
    {
        private bool toCelsius = false;

        public void setTargetUnit(bool toCelcius)
        {
            this.toCelsius = toCelcius;
        }

        public void setEventHandle(ref EventWaitHandle waitHandle)
        {
            this.waitHandle = waitHandle;
        }

        public override object[] convert(UInt16[] inValues)
        {
            object[] convertedValues = new object[4];

            int i = 0;
            foreach (UInt16 value in inValues)
            {
                convertedValues[i] = UNITS_Temp10CToF((Int16)value);

                if ((Int16)convertedValues[i] == -99)
                {
                    convertedValues[i] = "ERR";
                }
                i++;
            }
            return convertedValues;
        }

        /***************************************************************
        * Public                 getValues
        *
        * Sensor routine responsible for getting the 
        *
        * Inputs
        * None
        * 
        * Outputs
        * string[] values: Array of strings containing sensor values and units.
        * 
        * History:
        ****************************************************************/
       /* public override string[] getValues()
        {
            string[] values = new string[4];
            int i = 0;

            foreach (object temp in currentSensorValues)
            {
                switch (toCelsius)
                {
                    case true: values[i] = temp.ToString() + " C"; break;
                    case false: values[i] = temp.ToString() + " F"; break;
                }
                if (temp == "ERR")
                {
                    values[i] = temp.ToString();
                }

                i++;
            }

            updated = false;
            return values;
        }*/

        /***************************************************************
        * Public         UNITS_Temp10CToF
        *
        * Converts temperature from degrees C to F.  The input
        * temperature is assumed to be its value in degrees C * 10.
        * Example, input value = 1000 (100C*10) returns 212.
        *
        * Inputs
        *   INT16S temp10C - Temperature in degrees C * 10
        *
        * Outputs
        *   INT16S : Returns temp in degrees F  
        * 
        * History:
        ****************************************************************/
        Int16 UNITS_Temp10CToF(Int16 temp10C)
        {
            Int16 tempF;
            Int32 temp;

            temp = temp10C;
            temp *= 18;   /* temp10C * 1.8 * 10 (temperature in C * 100 * 1.8) */
            temp += 50;   /* Round up by 0.5 (must add 50 before dividing)     */
            temp /= 100;
            tempF = (Int16)temp;
            tempF += 32;
            return tempF;
        }
    }
}